package com.shxc.smarttrade.other.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.base.BaseActivity;
import com.shxc.smarttrade.main.MainActivity;
import com.shxc.smarttrade.other.adapter.GuideViewPagerAdapter;

import org.xchris.view.annotation.ContentView;
import org.xchris.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导页
 */
@ContentView(R.layout.activity_guide_c)
public class WelcomeGuideActivity extends BaseActivity implements View.OnClickListener {

    @ViewInject(R.id.guide_viewpager)
    ViewPager guide_viewpager;
    @ViewInject(R.id.linearlayout)
    LinearLayout linearlayout;
    @ViewInject(R.id.jump_textview)
    TextView jump_textview;

    Button enter_button;
    private GuideViewPagerAdapter adapter;

    private List<View> views = new ArrayList<View>();
    // 引导页图片资源
    private static final int[] pics = {R.layout.guide_view1,
            R.layout.guide_view2, R.layout.guide_view3, R.layout.guide_view4};

    // 底部小点图片
    private ImageView[] dots;

    // 记录当前选中位置
    private int currentIndex;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 初始化引导页视图列表

        for (int i = 0; i < pics.length; i++) {
            View view = LayoutInflater.from(this).inflate(pics[i], null);

            if (i == pics.length - 1) {
                enter_button = (Button) view.findViewById(R.id.enter_button);
                enter_button.setTag("enter");
                enter_button.setOnClickListener(this);
            }

            views.add(view);

        }

        adapter = new GuideViewPagerAdapter(views);
        guide_viewpager.setAdapter(adapter);
        guide_viewpager.addOnPageChangeListener(new PageChangeListener());

        jump_textview.setOnClickListener(this);

        initDots();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.enter_button:
            case R.id.jump_textview:
                JumpTo(MainActivity.class);
                finish();
                break;
            default:
                int position = (Integer) v.getTag();
                setCurView(position);
                setCurDot(position);
                break;
        }

    }

    private void initDots() {
        dots = new ImageView[pics.length];

        // 循环取得小点图片
        for (int i = 0; i < pics.length; i++) {
            // 得到一个LinearLayout下面的每一个子元素
            dots[i] = (ImageView) linearlayout.getChildAt(i);
            dots[i].setEnabled(false);// 都设为灰色
            dots[i].setOnClickListener(this);
            dots[i].setTag(i);// 设置位置tag，方便取出与当前位置对应
        }

        currentIndex = 0;
        dots[currentIndex].setEnabled(true); // 设置为白色，即选中状态
    }


    private class PageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int position) {

        }

        @Override
        public void onPageScrolled(int position, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int position) {
            // 设置底部小点选中状态
            setCurDot(position);
        }

    }

    /**
     * 设置当前view
     *
     * @param position
     */
    private void setCurView(int position) {
        if (position < 0 || position >= pics.length) {
            return;
        }
        guide_viewpager.setCurrentItem(position);
    }

    /**
     * 设置当前指示点
     *
     * @param position
     */
    private void setCurDot(int position) {
        if (position < 0 || position > pics.length || currentIndex == position) {
            return;
        }
        dots[position].setEnabled(true);
        dots[currentIndex].setEnabled(false);
        currentIndex = position;
    }
}
